import * as echarts from 'echarts'

// 图表配置常量
export const CHART_CONFIG = {
    // 通用配置
    common: {
        title: {
            textStyle: {
                fontSize: 16,
                fontWeight: 'bold',
                color: '#303133'
            }
        },
        tooltip: {
            textStyle: {
                fontSize: 12
            }
        },
        legend: {
            textStyle: {
                fontSize: 12
            }
        }
    },

    // 网格配置 - 优化宽度
    grid: {
        standard: {
            left: '10%',
            right: '10%',
            bottom: '20%',
            top: '20%',
            containLabel: true
        },
        wide: {
            left: '8%',
            right: '8%',
            bottom: '15%',
            top: '15%',
            containLabel: true
        },
        compact: {
            left: '12%',
            right: '12%',
            bottom: '25%',
            top: '25%',
            containLabel: true
        },
        heatmap: {
            height: '70%',
            top: '20%',
            left: '10%',
            right: '10%'
        }
    },

    // 轴标签配置
    axisLabel: {
        fontSize: 12,
        margin: 10,
        color: '#606266'
    },

    // 柱状图配置
    bar: {
        width: '50%', // 调整为正常宽度
        label: {
            fontSize: 12,
            color: '#303133'
        }
    },

    // 折线图配置
    line: {
        symbolSize: 6,
        lineStyle: {
            width: 2
        }
    },

    // 饼图配置
    pie: {
        radius: ['30%', '60%'], // 调整为正常大小
        center: ['60%', '50%'], // 右移为图例留空间
        emphasis: {
            label: {
                fontSize: '18',
                fontWeight: 'bold'
            }
        }
    }
}

// 颜色配置
export const COLORS = {
    primary: ['#5470c6', '#91cc75', '#ee6666', '#73c0de', '#3ba272', '#fc8452'],
    warning: ['#f56c6c', '#e6a23c', '#409eff', '#67c23a', '#909399'],
    gradient: ['#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#ffffcc', '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
}

// 统一初始化图表函数
export const initChart = (element, option) => {
    if (!element) return null

    const chart = echarts.init(element)

    // 应用通用配置
    if (option.title) {
        option.title = { ...option.title, ...CHART_CONFIG.common.title }
    }
    if (option.tooltip) {
        option.tooltip = { ...option.tooltip, ...CHART_CONFIG.common.tooltip }
    }
    if (option.legend) {
        option.legend = { ...option.legend, ...CHART_CONFIG.common.legend }
    }

    // 应用网格配置
    if (!option.grid) {
        option.grid = CHART_CONFIG.grid.standard
    }

    chart.setOption(option)

    // 监听窗口大小变化
    window.addEventListener('resize', () => {
        chart.resize()
    })

    return chart
}

// 优化图表选项
export const optimizeChartOption = (option) => {
    // 确保所有系列都有合适的样式
    if (option.series) {
        option.series.forEach(series => {
            if (series.type === 'bar') {
                series.barWidth = series.barWidth || CHART_CONFIG.bar.width
                if (series.label) {
                    series.label = { ...CHART_CONFIG.bar.label, ...series.label }
                }
            }
            if (series.type === 'line') {
                series.symbolSize = series.symbolSize || CHART_CONFIG.line.symbolSize
                series.lineStyle = { ...CHART_CONFIG.line.lineStyle, ...series.lineStyle }
            }
            if (series.type === 'pie') {
                series.radius = series.radius || CHART_CONFIG.pie.radius
                series.center = series.center || CHART_CONFIG.pie.center
                if (series.emphasis && series.emphasis.label) {
                    series.emphasis.label = { ...CHART_CONFIG.pie.emphasis.label, ...series.emphasis.label }
                }
            }
        })
    }

    // 确保网格配置正确
    if (option.grid) {
        option.grid = { ...CHART_CONFIG.grid.standard, ...option.grid }
    }

    return option
}

// 获取响应式图表高度
export const getChartHeight = (screenWidth) => {
    if (screenWidth >= 1200) {
        return 450
    } else if (screenWidth >= 768) {
        return 400
    } else {
        return 350
    }
}

// 销毁图表
export const destroyChart = (chart) => {
    if (chart) {
        chart.dispose()
    }
} 